'use strict';
var TopClient = require('../ali/topClient').TopClient;
var client = new TopClient({
  'appkey': '23454774',
  'appsecret': '6369292686cc2064c3059844d144b25c',
  'REST_URL': 'http://gw.api.taobao.com/router/rest'
});

module.exports = function(用户验证码) {
  var re = /^1(3|4|5|7|8)\d{9}$/;
  用户验证码.validatesFormatOf('手机号码', {with: re, message: '手机号码填写错误'});
  用户验证码.getyzm=function(手机号,cb){
    var numOutput="";
    for(var i=0;i<4;i++)
    {
      numOutput+=Math.floor(Math.random()*10);
    }
    client.execute('alibaba.aliqin.fc.sms.num.send', {
      'extend':手机号,
      'sms_type':'normal',
      'sms_free_sign_name':'蓝典科技',
      'sms_param':'{"code":"'+numOutput+'","product":"云药追溯APP"}',
      'rec_num':手机号,
      'sms_template_code':'SMS_13036617'
    }, function(error, response) {
      if (!error) {
        用户验证码.destroyAll({"手机号码":手机号},function(err,obj){
          用户验证码.create({"手机号码":手机号,"验证码":numOutput},function(err,obj){
            if(err)
            {
              cb(null,err.message);
            }else{
              cb(null,"验证码已经发送!");
            }
          });
        });
      }
      else { cb(null,"SDK调用失败!");}
    })
  }
  用户验证码.remoteMethod('getyzm',
    {
      description: '注册验证码',
      accepts: [{arg: '手机号', type: 'string'}],
      returns: {arg: 'success', type: 'string', root: true, description: '返回描述'},
      http: {verb: 'get',path: '/getyzm'}
    }
  );
};
